package com.dxids.les.common;

import androidx.annotation.NonNull;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import honeywell.connection.ConnectionBase;
import honeywell.connection.Connection_Bluetooth;

public class Printer implements MethodChannel.MethodCallHandler {

    private static final String channel = "printer";

    public static void createChannel(BinaryMessenger binaryMessenger) {
        new MethodChannel(binaryMessenger, channel).setMethodCallHandler(
                new Printer()
        );
    }

    @Override
    public void onMethodCall(MethodCall methodCall, @NonNull MethodChannel.Result result) {
        switch (methodCall.method) {
            case "printCmd":
                printCmd(result, methodCall.argument("mac"), methodCall.argument("cmd"));
                break;
            case "printCmds":
                printCmds(result, methodCall.argument("mac"), methodCall.argument("cmds"));
                break;
            default:
                result.notImplemented();
                break;
        }
    }

    /**
     * 打印单条指令
     *
     * @param result 返回结果
     * @param mac    蓝牙打印机mac地址
     * @param cmd    打印指令
     */
    public void printCmd(MethodChannel.Result result, String mac, String cmd) {
        if (StrUtil.isEmpty(mac)) {
            result.error("00-00", "未获取到MAC地址!", StrUtil.EMPTY);
        }
        if (StrUtil.isEmpty(cmd)) {
            result.error("00-00", "未获取到打印指令!", StrUtil.EMPTY);
        }
        try {
            ConnectionBase conn = Connection_Bluetooth.createClient(mac, false);
            boolean connected = false;
            for (int i = 0; i < 3; i++) {
                if (!conn.getIsOpen()) {
                    conn.open();
                    connected = true;
                    break;
                }
            }
            if (!connected) {
                result.error("00-00", mac + "打印机连接异常!", StrUtil.EMPTY);
            }
            conn.write(cmd);
            conn.close();
        } catch (Exception e) {
            result.error("00-00", "打印失败!", e.getMessage());
        }
        result.success("打印成功");
    }

    /**
     * 批量打印指令
     *
     * @param result 返回结果
     * @param mac    蓝牙打印机mac地址
     * @param cmds   打印指令
     */
    public void printCmds(MethodChannel.Result result, String mac, List<String> cmds) {
        if (StrUtil.isEmpty(mac)) {
            result.error("00-00", "未获取到MAC地址!", StrUtil.EMPTY);
        }
        if (CollUtil.isEmpty(cmds)) {
            result.error("00-00", "未获取到打印指令!", StrUtil.EMPTY);
        }
        try {
            ConnectionBase conn = Connection_Bluetooth.createClient(mac, false);
            boolean connected = false;
            for (int i = 0; i < 3; i++) {
                if (!conn.getIsOpen()) {
                    conn.open();
                    connected = true;
                    break;
                }
            }
            if (!connected) {
                result.error("00-00", mac + "打印机连接异常!", StrUtil.EMPTY);
            }
            for (String cmd : cmds) {
                conn.write(cmd);
            }
            conn.close();
        } catch (Exception e) {
            result.error("00-00", "打印失败!", e.getMessage());
        }
        result.success("打印成功");
    }

}